package top.xcxdl.rural.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import top.xcxdl.rural.common.Res;
import top.xcxdl.rural.entity.RuralProduct;
import top.xcxdl.rural.entity.RuralUser;
import top.xcxdl.rural.service.IRuralProductService;
import top.xcxdl.rural.utils.JWTUtils;
import top.xcxdl.rural.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 后台管理产品控制器
 * </p>
 *
 * @author Ding
 * @since 2022-05-06
 */
@Api("后台管理产品控制器")
@RestController
@RequestMapping("/admin/product")
public class BackRuralProductController {

    @Resource
    private IRuralProductService service;

    /**
     * 后台获取所有产品
     * @param pageNo 页码
     * @param pageSize 每页数量
     * @param productStatus 产品状态
     * @param authorType 作者类型
     * @param key 搜索关键字
     * @return 产品列表
     */
    @ResponseBody
    @GetMapping("/getList")
    @ApiOperation("后台获取所有产品")
    public Res<Object> getList(
            @RequestParam(name = "page_no") Integer pageNo,
            @RequestParam(name = "page_size") Integer pageSize,
            @RequestParam(name = "product_status", required = false) Integer productStatus,
            @RequestParam(name = "product_type", required = false) Integer productType,
            @RequestParam(name = "author_type", required = false) Integer authorType,
            @RequestParam(name = "title",required = false) String key
    ) {
        PageInfo<RuralProduct> pageInfo = service.getList(pageNo, pageSize, productStatus,productType, authorType, key);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return Res.success(jsonObject);
    }

    /**
     * 后台新增 or 修改（处理）产品宣传信息
     * @param product 产品宣传实体
     * @return res
     */
    @PostMapping("/doEdit")
    @ApiOperation("后台新增 or 修改（处理）产品宣传信息")
    @ResponseBody
    public Res<Object> doEdit(
            @RequestBody RuralProduct product){
        int ret = 0;
        Integer id = product.getProductId();
        if (id == null) {
            //新增,设置创建人
            Integer adminUserId = JWTUtils.getAuthorId();;
            product.setAuthorType(1);
            product.setAuthorId(adminUserId);
            product.setProductStatus(1);
        }
        ret += service.doEdit(product);
        return ret > 0 ? Res.success(id == null ? "添加成功" : "编辑成功") : Res.error("操作失败");
    }

    @ApiOperation("后台删除产品")
    @PostMapping("/doDelete")
    @ResponseBody
    public Res<Object> delete(@RequestBody JSONObject jsonObject) {
        //获取id集合
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        //删除
        int ret = service.delete(ids);
        return ret > 0 ? Res.success("删除成功") : Res.error("删除失败");
    }

    /**
     * 后台获取产品详情
     * @param productId 产品id
     * @return 产品详情实体
     */
    @ApiOperation("后台获取产品详情")
    @GetMapping("/getDetail")
    @ResponseBody
    public Res<RuralProduct> getDetail(@RequestParam(name = "product_id") Integer productId) {
        RuralProduct product = service.getDetail(productId);
        return Res.success(product);
    }
}
